package com.sisgesbyte.dao.comun;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sisgesbyte.dao.beanDao.EstadoRequest;
import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.EstadoEntity;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoEstadoDaoImpl extends AbstractFacade<EstadoEntity> implements MantenimientoEstadoDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoEstadoDaoImpl(){
		super(EstadoEntity.class);
	}
	
	@Override
	public List<EstadoEntity> listaEstados(FiltrosSQLBean cadenaFiltros) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT id, nombre, active FROM administracion.estado ");
		
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), EstadoEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<EstadoEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarEstado(EstadoRequest request) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT * from comun.sp_registrar_estado(:in_nombre, :in_active)");

		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_nombre", request.getNombre());
		query.setParameter("in_active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarEstado(EstadoRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from comun.sp_actualizar_estado(:in_id, :in_nombre, :in_active)");

		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", request.getId());
		query.setParameter("in_nombre", request.getNombre());
		query.setParameter("in_active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarEstado(int codEstado) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from comun.sp_eliminar_estado(:in_id)");

		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", codEstado);

		return (GenericEntity) query.getSingleResult();
	}

}
